//
//  CJProductDetailImageCell.m
//  Boss
//
//  Created by admin on 2018/9/17.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "CJProductDetailImageCell.h"
#import "CJAddFirmProductImageCell.h"
@interface CJProductDetailImageCell()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (strong, nonatomic) IBOutlet UICollectionView *collectionView;

@property (strong, nonatomic) IBOutlet NSLayoutConstraint *collectionViewHeight;

@end
@implementation CJProductDetailImageCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    [self setUpUI];
}

-(void)setDataArray:(NSMutableArray *)dataArray
{
    _dataArray = dataArray;
    [self reloadCell];
}

- (void)reloadCell{
    [self.collectionView reloadData];
    
    if (self.collectionViewHeight.constant !=self.collectionView.collectionViewLayout.collectionViewContentSize.height) {
        
        dispatch_async(dispatch_get_main_queue(), ^{
            // UI更新代码
            self.collectionViewHeight.constant = self.collectionView.collectionViewLayout.collectionViewContentSize.height;
            if ([self.delegate respondsToSelector:@selector(didChangeHeight:)]) {
                [self.delegate didChangeHeight:self];
            }
        });
        
        
    }
   
    //    [self.collectionView mas_updateConstraints:^(MASConstraintMaker *make) {
    //        make.height.equalTo(@(self.collectionView.collectionViewLayout.collectionViewContentSize.height));
    //    }];
    
    // [self.delegate didChangeCell:self];
}
-(void)setUpUI
{
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
    layout.itemSize=CGSizeMake(kScreenWidth-32, (kScreenWidth-32)/343*192);
    //横向最小距离
    layout.minimumInteritemSpacing=5.0f;
    layout.minimumLineSpacing=5.0f;
    //cell距离上  左  下 右 的距离
    layout.sectionInset=UIEdgeInsetsMake(16, 16, 0, 0);
    [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
    [self.collectionView setCollectionViewLayout:layout];
    [self.collectionView registerNib:[UINib nibWithNibName:@"CJAddFirmProductImageCell" bundle:nil] forCellWithReuseIdentifier:@"CJAddFirmProductImageCell"];
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
//    if (self.dataArray.count>=9) {
//        return 9;
//    }else{
//        return self.dataArray.count+1;
//    }
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CJAddFirmProductImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CJAddFirmProductImageCell" forIndexPath:indexPath];
    
    if (!cell) {
        cell = [[NSBundle mainBundle]loadNibNamed:@"CJAddFirmProductImageCell" owner:nil options:nil].firstObject;
    }
     cell.deleteBtn.hidden = YES;
     [cell.productImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@!default",self.dataArray[indexPath.row]]] placeholderImage:[UIImage imageNamed:@"logo_place"]];
    
    
    
    
//    if (self.dataArray.count>=9) {
//        cell.deleteBtn.hidden = NO;
//        if ([self.dataArray[indexPath.row] isKindOfClass:[UIImage class]]) {
//            cell.productImageView.image =self.dataArray[indexPath.row];
//        }
//        if ([self.dataArray[indexPath.row] isKindOfClass:[NSString class]]) {
//            [cell.productImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@!default",self.dataArray[indexPath.row]]] placeholderImage:[UIImage imageNamed:@"logo_place"]];
//        }
//    }else{
//        if (indexPath.row == self.dataArray.count) {
//            cell.deleteBtn.hidden = YES;
//            cell.productImageView.image =[UIImage imageNamed:@"uploadPage"];
//        }else{
//            cell.deleteBtn.hidden = NO;
//            if ([self.dataArray[indexPath.row] isKindOfClass:[UIImage class]]) {
//                cell.productImageView.image =self.dataArray[indexPath.row];
//            }
//            if ([self.dataArray[indexPath.row] isKindOfClass:[NSString class]]) {
//                [cell.productImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@!default",self.dataArray[indexPath.row]]] placeholderImage:[UIImage imageNamed:@"logo_place"]];
//            }
//
//
//
//        }
//
//    }
//
    
    
    
    
    
    
    
    
//    cell.deleteBtn.tag = indexPath.row;
//    [cell.deleteBtn addTarget:self action:@selector(deleteBtnClik:) forControlEvents:UIControlEventTouchUpInside];
    
    return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//    if ([self.delegate respondsToSelector:@selector(addProductImageView)]) {
//        [self.delegate addProductImageView];
//    }
}




- (void)deleteBtnClik:(UIButton *)sender {
    
//    if ([self.delegate respondsToSelector:@selector(deleteImageClick:)]) {
//        [self.delegate deleteImageClick:sender.tag];
//    }
    
}
@end
